// http.post('http://f997a4.v.vote8.cn/m')

var http=require('http');  
var querystring=require('querystring');  
var $ = require('jquery');

var hiddenVote8ClickValidateCode = '';
var arg2 = '';
function main() {
    http.get('http://f997a4.v.vote8.cn/m', function(req) {
        var html = '';
        req.on('data',function(data){  
            html+=data;  
        });  
        req.on('end',function(){  

            var index = html.indexOf('hiddenTimeStampEncodeString');
            var subString = html.substring(index, index + 120);
            console.log(subString);
            var lindex = subString.indexOf('value=');
            subString = subString.substring(lindex, lindex + 55);
            var start = subString.indexOf('"');
            var end = subString.lastIndexOf('"');
            var res = subString.substring(start + 1, end);
            console.log(end);
            console.log(start);
            console.log(subString);
            console.log(res);
            arg2 = res;
            setTimeout(get_hiddenVote8ClickValidateCode, 2000);

        });
    });
}
var get_hiddenVote8ClickValidateCode = function() {
    http.get('http://f997a4.v.vote8.cn/Front/VerifyCodeImage/Vote8Click.ashx', function(req, res) {
        var html = '';
        req.on('data',function(data){  
            html+=data;  
        });  
        req.on('end',function(){  
            console.info(html);  
            hiddenVote8ClickValidateCode = html;
            postReq(html, arg2);
        });  
    });
}
//发送 http Post 请求  
/**
 * 
__EVENTTARGET=ctl00$content$lbtnVote&
__EVENTARGUMENT=&
__VIEWSTATE=/wEPDwUKMTMxNjYyNzQxMWRkosTcN+7YALO5DiqEHq1nWeKse4c=&
__VIEWSTATEGENERATOR=C9E13C34&
VoteOption_2818832=6780466&
VoteOption_2818832=6780467&
VoteOption_2818832=6780468&
ctl00$content$rptTopicList$ctl00$hiddenTopicID=2818832&
VoteOption_2818835=6780411&
VoteOption_2818835=6780417&
VoteOption_2818835=6780424&
ctl00$content$rptTopicList$ctl01$hiddenTopicID=2818835&
ctl00$content$hiddenWeixinUserInfoJson=&
ctl00$content$hiddenWeixinUserInfoJsonEncode=&
hiddenVote8ClickValidateCode=1505222438,1d027abb8ef472cee7bb608f3dd6081a&
ctl00$content$ucVerifyCode$hiddenVerifyCodeModeInfo=8,16ed75c7b61b637ea8796923f333f388&
ctl00$content$hiddenRefererUrl=http://fj.qq.com/zt2017/movzan/2017wlchina.html%3Ffrom=groupmessage%26isappinstalled=0&
ctl00$content$hiddenTimeStampEncodeString=1505222423,035b9e56bde81403dcc8a2a749bfdaf1&
ctl00$content$hiddenLatitude=&
ctl00$content$hiddenLongitude=&
ctl00$content$hiddenGeoLocationEncode=
 */

 var postReq = function(arg1, arg2) {
    var postData=querystring.stringify({  
        __EVENTTARGET: 'ctl00$content$lbtnVote',
        __EVENTARGUMENT: '',
        __VIEWSTATE: '/wEPDwUKMTMxNjYyNzQxMWRkosTcN+7YALO5DiqEHq1nWeKse4c=',
        __VIEWSTATEGENERATOR:'C9E13C34',
        VoteOption_2818832: 6780466,
        VoteOption_2818832: 6780467,
        VoteOption_2818832: 6780468,
        ctl00$content$rptTopicList$ctl00$hiddenTopicID: 2818832,
        VoteOption_2818835: 6780411,
        VoteOption_2818835: 6780417,
        VoteOption_2818835: 6780424,
        ctl00$content$rptTopicList$ctl01$hiddenTopicID: 2818835,
        ctl00$content$hiddenWeixinUserInfoJson: '',
        ctl00$content$hiddenWeixinUserInfoJsonEncode: '',
        hiddenVote8ClickValidateCode: arg1,///http://f997a4.v.vote8.cn/Front/VerifyCodeImage/Vote8Click.ashx
        ctl00$content$ucVerifyCode$hiddenVerifyCodeModeInfo: '8,16ed75c7b61b637ea8796923f333f388',
        ctl00$content$hiddenRefererUrl: 'http://fj.qq.com/zt2017/movzan/2017wlchina.html%3Ffrom=groupmessage%26isappinstalled=0',
        ctl00$content$hiddenTimeStampEncodeString: arg2,//// 获取
        ctl00$content$hiddenLatitude: '',
        ctl00$content$hiddenLongitude: '',
        ctl00$content$hiddenGeoLocationEncode: '',
    });  
    var options={  
        hostname:'f997a4.v.vote8.cn',  
        port:80,  
        path:'/m',  
        method:'POST',  
        headers:{  
            //'Content-Type':'application/x-www-form-urlencoded',  
            'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',  
            'Content-Length':Buffer.byteLength(postData),
            'Cache-Control': 'max-age=0',
            'Origin':'http://f997a4.v.vote8.cn',
            'Upgrade-Insecure-Requests':	1,
            'User-Agent': 'Mozilla/5.0 (Linux; Android 7.1.1; OD105 Build/NMF26F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.125 Mobile Safari/537.36',
            'Content-Type': 'application/x-www-form-urlencoded',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'DNT':	1,
            'Referer':	'http://f997a4.v.vote8.cn/m',
            'Accept-Encoding':	'gzip, deflate',
            'Accept-Language':	'zh-CN,zh;q=0.8,en;q=0.6',
            'Cookie': '__cfduid=d34cbb77e1a58eff8ec99040a1860f4191505175702; UM_distinctid=15e737695efd9-0916a06cca28fd-411f3f1a-38400-15e737695f0a6; ASP.NET_SessionId=ouql4jsd4tpdme2sylp3taqq; Vote.VoteHistory.2846706=2017/9/12,4; Vote.HasVote.2846706=2017/9/12 19:07:54; Vote.HasVoteJustNow=1; CNZZDATA5855278=cnzz_eid%3D1449200005-1505170862-null%26ntime%3D1505218529',
            'Content-Encoding': 'gzip',
        },
        gzip: true,  
    }  


    var req=http.request(options, function(res) {  
        console.log('Status:',res.statusCode);  
        console.log('headers:',JSON.stringify(res.headers));  
        // res.setEncoding('utf-8');  
        var html111 = '';
        res.on('data',function(chun){  
            console.log('body分隔线---------------------------------\r\n');  
            html111 += chun;
        });  
        res.on('end',function(){  
            console.log('No more data in response.********');  
            console.log(html111);
        });  
    });  
    req.on('error',function(err){  
        console.error(err);  
    });  
    req.write(postData);  
    req.end(); 

 }


 main();